import api from './api.js'
var pingpp = require('../utils/pingpp.js');
var util = require('../utils/util.js');

//获取支付凭证
function request_charge(params, successCallback, errorCallback) {
  var charge = null;
  api.getCharge(params, (res) => { // 获取支付凭证
    charge = res.data.charge;
    pingpp.createPayment(charge, function(result, err) { // 调起ping++ 微信支付接口
      if (result == "success") {
        api.getPayResult(orderCode, (res) => { // 根据返回结果，支付成功，向服务器拉取支付结果
          util.isFunction(successCallback) && successCallback(res);
        }, () => {
          util.isFunction(errorCallback) && errorCallback();
        }, null)
      } else {
        util.isFunction(errorCallback) && errorCallback(result + " " + err.msg + " " + err.extra);
      }
    })
  }, () => {
    util.isFunction(errorCallback) && errorCallback();
  }, null);
}
module.exports = {
  request_charge: request_charge,
};